package com.tuo.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * 作者：Tuo on 2018/4/13 16:35
 * 邮箱：839539179@qq.com
 */

public class PwdTextView extends AppCompatTextView {
    //是否设置高度=宽度
    private boolean isSquare;
    private float radius;

    private boolean hasPwd;
    private Paint paint;// 画一个黑色的圆
    private String content;

    public PwdTextView(Context context) {
        this(context, null);
    }

    public PwdTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PwdTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 画一个黑色的圆
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isSquare) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (hasPwd) {
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
        }
    }


    public void clearPwd() {
        this.hasPwd = false;
        invalidate();
    }

    public void setContent(String con) {
        content = con;
    }

    public String getContent() {
        if (TextUtils.isEmpty(content)) {
            return "";
        }
        return content;
    }

    public void drawPwd(float radius) {
        this.hasPwd = true;
        if (radius == 0) {
            this.radius = getWidth() / 4;
        } else {
            this.radius = radius;
        }
        invalidate();
    }

    public void setSquare(boolean square) {
        isSquare = square;
    }
}
